package com.example.fangyh09.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
    TextView textView;
    Spinner spinner;
    List<String> list;
    ArrayAdapter<String> adapter;

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String cityname = adapter.getItem(position);
        textView.setText("You choose"  +cityname);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView)findViewById(R.id.textview);
        spinner = (Spinner)findViewById(R.id.spinner);
        list = new ArrayList<String>();
        textView.setText("You choose beijing");
        list.add("beijing");
        list.add("shanghai");
        list.add("guangzhou");
        list.add("shenzheng");

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);


    }
}































